﻿Imports System.Data.SqlClient
Public Class clsReportesOperadores
    Private mintidReporte As Integer
    Private mintidconsecutivo As Integer
    Private mintidEmpleado As Integer
    Private mdatFecha As Date
    Private mstrMotivo As String
    Private mstrResolucion As String

    Public Property idReporte() As Integer
        Get
            Return mintidReporte
        End Get
        Set(ByVal value As Integer)
            mintidReporte = value
        End Set
    End Property

    Public Property idConsecutivo() As Integer
        Get
            Return mintidconsecutivo
        End Get
        Set(ByVal value As Integer)
            mintidconsecutivo = value
        End Set
    End Property


    Public Property idempleado() As Integer
        Get
            Return mintidEmpleado
        End Get
        Set(ByVal value As Integer)
            mintidEmpleado = value
        End Set
    End Property
    Public Property fecha() As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
    Public Property Motivo() As String
        Get
            Return mstrMotivo
        End Get
        Set(ByVal value As String)
            mstrMotivo = value
        End Set
    End Property

    Public Property Resolucion() As String
        Get
            Return mstrResolucion
        End Get
        Set(ByVal value As String)
            mstrResolucion = value
        End Set
    End Property

    Public Sub New()
        mintidReporte = 0
        mintidconsecutivo = 0
        mintidEmpleado = 0
        mdatFecha = Now()
        mstrmotivo = ""
        mstrResolucion = ""
    End Sub
    Public Sub Guardar()

        If idReporte = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer

        intRetVal = BuscarIntDatoCitra("Reportes", "idReporte", "idReporte=" & idReporte)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()

        End If

    End Sub
    Public Sub Borrar()
        Call SetSqlCitra("Delete From Reportes Where idreporte= " & idReporte)

    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Reportes " & _
        " WHERE idreporte=" & idReporte

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mintidReporte = rdBuscar("idreporte")
            mintidconsecutivo = rdBuscar("idconsecutivo")
            mintidEmpleado = rdBuscar("idempleado")
            mdatFecha = rdBuscar("fecha")
            mstrMotivo = rdBuscar("motivo").ToString.Trim
            mstrResolucion = rdBuscar("resolucion").ToString.Trim
        Else
            mintidReporte = 0
            mintidconsecutivo = 0
            mintidEmpleado = 0
            mdatFecha = Now()
            mstrMotivo = ""
            mstrResolucion = ""
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Reportes (" & _
                            "IdReporte," & _
                            "IdConsecutivo," & _
                            "IdEmpleado," & _
                            "Fecha," & _
                            "Motivo," & _
                            "Resolucion)" & _
                            "VALUES (" & _
                            "@IdReporte," & _
                            "@IdConsecutivo," & _
                            "@IdEmpleado," & _
                            "@Fecha," & _
                            "SUBSTRING(@Motivo,1,100)," & _
                            "SUBSTRING(@Resolucion,1,200))"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdReporte", mintidReporte)
        Command.Parameters.AddWithValue("IdConsecutivo", mintidconsecutivo)
        Command.Parameters.AddWithValue("IdEmpleado", mintidEmpleado)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("Motivo", mstrMotivo)
        Command.Parameters.AddWithValue("Resolucion", mstrResolucion)
       

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update Reportes Set " & _
                    "Fecha=@Fecha," & _
                    "Motivo=@Motivo," & _
                    "Resolucion=@Resolucion," & _
                    "Where IdReporte=@IdReporte"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdReporte", mintidReporte)
        Command.Parameters.AddWithValue("IdConsecutivo", mintidconsecutivo)
        Command.Parameters.AddWithValue("IdEmpleado", mintidEmpleado)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("Motivo", mstrMotivo)
        Command.Parameters.AddWithValue("Resolucion", mstrResolucion)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el liquidación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If

    End Sub
   
End Class
